package com.foreknow.elm.controller;

import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.service.impl.DeliveryaddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-27 10:21
 */
public class DeliveryAddressController {
    /**
     * DeliveryAddressController/listDeliveryAddressByUserId
     *    参数：userId
     *    返回值：deliveryAddress数组
     *    功能：根据用户编号查询所属送货地址
     */
    public List<Deliveryaddress> listDeliveryAddressByUserId(HttpServletRequest request)throws Exception{
        //获取请求参数
        String userId=request.getParameter("userId");
        //调用Service方法
        DeliveryaddressService service=new DeliveryaddressServiceImpl();
        List<Deliveryaddress> list = service.listDeliveryAddressByUserId(userId);

        return list;


    }


    /**
     * DeliveryAddressController/getDeliveryAddressById
     *    参数：daId
     *    返回值：deliveryAddress对象
     *    功能：根据送货地址编号查询送货地址
     */
    public Deliveryaddress getDeliveryAddressById(HttpServletRequest request)throws Exception{
        //获取请求参数
        int daId=Integer.parseInt(request.getParameter("daId"));

        //调用Service方法
        DeliveryaddressService service=new DeliveryaddressServiceImpl();
        Deliveryaddress deliveryAddressById = service.getDeliveryAddressById(daId);
        return deliveryAddressById;
    }

    /**
     *  DeliveryAddressController/saveDeliveryAddress
     *    参数：contactName、contactSex、contactTel、address、userId
     *    返回值：int（影响的行数）
     *    功能：向送货地址表中添加一条记录
     */
    public int saveDeliveryAddress(HttpServletRequest request)throws Exception{
        //获取请求参数
        String contactName=request.getParameter("contactName");
        int  contactSex=Integer.parseInt(request.getParameter("contactSex"));
        String contactTel=request.getParameter("contactTel");
        String address=request.getParameter("address");
        String userId=request.getParameter("userId");

        //调用Service方法
        DeliveryaddressService service=new DeliveryaddressServiceImpl();
        int i = service.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
        return i;
    }


    /**
     *  DeliveryAddressController/updateDeliveryAddress
     *    参数：daId、contactName、contactSex、contactTel、address、userId
     *    返回值：int（影响的行数）
     *    功能：根据送货地址编号更新送货地址信息
     */
    public  int updateDeliveryAddress(HttpServletRequest request)throws Exception{
        //获取请求参数
        int  daId=Integer.parseInt(request.getParameter("daId"));
        String contactName=request.getParameter("contactName");
        int  contactSex=Integer.parseInt(request.getParameter("contactSex"));
        String contactTel=request.getParameter("contactTel");
        String address=request.getParameter("address");
        String userId=request.getParameter("userId");

        //调用Service方法
        DeliveryaddressService service=new DeliveryaddressServiceImpl();
        int i = service.updateDeliveryAddress(daId, contactName, contactSex, contactTel, address, userId);
        return i;

    }


    /**
     * DeliveryAddressController/removeDeliveryAddress
     *    参数：daId
     *    返回值：int（影响的行数）
     *    功能：根据送货地址编号删除一条记录
     */
    public int removeDeliveryAddress(HttpServletRequest request)throws Exception{
        //获取请求参数
        int  daId=Integer.parseInt(request.getParameter("daId"));
        //调用Service方法
        DeliveryaddressService service=new DeliveryaddressServiceImpl();
        int i = service.removeDeliveryAddress(daId);
        return i;

    }
}
